using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TaugeLibrary.Interfaces;
using TaugeLibrary.Classes.Abstracts;
using TaugeLibrary.Classes;
using System.Reflection;
using TaugeLibrary.Classes.Attributes;
using TaugeLibrary.Enums;

namespace TaugeLibrary.UserControls
{
    public partial class ABusinessUnitUI : UserControl
    {
        ITaugeFramework m_framework = null;
        ABusinessUnit m_unit;
        public ABusinessUnitUI()
        {
            InitializeComponent();
        }

        public ABusinessUnitUI(ITaugeFramework p_framework, ABusinessUnit p_unit):this()
        {            
            m_framework = p_framework;
            m_unit = p_unit;
        }
        
        public virtual void writeUI(ABusinessUnit p_unit)
        {
            m_framework.writeUI(p_unit, this);
        }

        public virtual void readUI(ABusinessUnit p_unit)
        {            
            m_framework.readUI(p_unit, this);
        }

        public virtual void setViewMode(eViewMode p_view_mode)
        {
            PropertyInfo[] pis = m_unit.GetType().GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                AttBindControl _bind = (AttBindControl)LibConstants.getAttribute(pi, typeof(AttBindControl));
                if (_bind != null && _bind.ControlType != eControlType.PROPERTY)
                {
                    bool _enabling = true;

                    if (p_view_mode == eViewMode.UPDATE)
                    {
                        //key
                        AttKeyField _keyfield = (AttKeyField)LibConstants.getAttribute(pi, typeof(AttKeyField));
                        if(_keyfield!=null)
                            _enabling = false;
                    }

                    if (p_view_mode == eViewMode.QUERY || p_view_mode == eViewMode.DELETE)
                    {
                        _enabling = false;
                    }
                    else if (p_view_mode == eViewMode.SEARCH)
                    {
                        AttSearchable _searchfield = (AttSearchable)LibConstants.getAttribute(pi, typeof(AttSearchable));
                        if (_searchfield == null)
                            _enabling = false;                        
                    }
                    switch (_bind.ControlType)
                    {
                        case eControlType.CHECKBOX:                           
                        case eControlType.COMBOBOX:
                            Control _control = LibConstants.getControl(this, _bind.ControlName);
                            if (_control != null)
                            {
                                _control.Enabled = _enabling;

                                if (p_view_mode == eViewMode.SEARCH && _enabling)
                                {
                                    if (_control is CheckBox)
                                    {
                                        CheckBox _chkbox = (CheckBox)_control;
                                        _chkbox.ThreeState = true;
                                        _chkbox.CheckState = CheckState.Indeterminate;

                                    }
                                    else if (_control is ComboBox)
                                    {
                                        bool _hasnull = false;
                                        ComboBox _combobox = (ComboBox)_control;

                                        foreach (TaugeListItem i in _combobox.Items)
                                        {
                                            if (i.Value == null)
                                            {
                                                _hasnull = true;
                                                break;
                                            }
                                        }
                                        if (!_hasnull)
                                            _combobox.Items.Add(new TaugeListItem("", null));

                                        _combobox.SelectedValue = null;

                                    }
                                }
                                else if(p_view_mode!= eViewMode.SEARCH)
                                {
                                    if(_control is CheckBox)
                                    {
                                        CheckBox _chkbox = (CheckBox)_control;
                                        _chkbox.ThreeState = false;                                        
                                    }
                                    else if (_control is ComboBox)
                                    {
                                        ComboBox _combobox = (ComboBox)_control;
                                        foreach (TaugeListItem i in _combobox.Items)
                                        {
                                            if (i.Value == null)
                                                _combobox.Items.Remove(i);                                                                                            
                                        }
                                    }   
                                }
                            }
                            
                            break;
                        case eControlType.TEXTBOX:
                            Control _txtbox = LibConstants.getControl(this, _bind.ControlName);
                            if(_txtbox!=null)
                            {
                                if(_txtbox is TextBox)
                                    ((TextBox)_txtbox).ReadOnly = !_enabling;
                            }
                            break;
                    }
                }
                
                
            }
            

        }

        
    }
}
